﻿@inject NavigationManager Navigation
@inject IWorkService workService
@inject IUserService userService
@inject ContextMenuService contextMenuService

@implements IDisposable

@if( ProcessingStatus != null )
{
    <div class="damselfly-statusbarpanel" title="@StatusTitle"
         @oncontextmenu=@(args => ShowContextMenu(args)) @oncontextmenu:preventDefault="true"
         @onclick="@(args => ShowContextMenu(args))">
        <i class="processing-icon fas @StatusIcon"/>
        <label>@ProcessingStatus.StatusText</label>
    </div>
}

@code{

    [CascadingParameter] private Task<AuthenticationState> authenticationStateTask { get; set; }

    private ServiceStatus ProcessingStatus { get; set; }

    private string StatusIcon => ProcessingStatus.Status switch
    {
        JobStatus.Running => $"fa-cog {SpinSpeed}",
        JobStatus.Paused => "fa-pause-circle",
        JobStatus.Idle => "fa-check-circle",
        JobStatus.Disabled => "fa-stop-circle",
        JobStatus.Error => "fa-exclamation-circle",
        _ => throw new ArgumentException("StatusIcon: Unexpected Switch option")
    };

    private string SpinSpeed => ProcessingStatus.CPULevel switch
    {
        25 => "fa-spin-25",
        50 => "fa-spin-50",
        75 => "fa-spin-75",
        100 => "fa-spin-100",
        _ => throw new ArgumentException("SpinSpeed: Unexpected Switch option")
    };

    private string StatusTitle => ProcessingStatus.Status switch
    {
        JobStatus.Running => $"Background processing running (CPU: {ProcessingStatus.CPULevel}%)",
        JobStatus.Paused => "Background processing paused",
        JobStatus.Idle => "Background processing idle",
        JobStatus.Disabled => "Background processing disabled",
        JobStatus.Error => "Background processing error",
        _ => throw new ArgumentException("StatusTitle: Unexpected Switch option")
    };

    private void StatusChanged(ServiceStatus status)
    {
        InvokeAsync(() =>
        {
            ProcessingStatus = status;
            StateHasChanged();
        });
    }

    protected override async Task OnInitializedAsync()
    {
        ProcessingStatus = await workService.GetWorkStatus();
        workService.OnStatusChanged += StatusChanged;

        await base.OnInitializedAsync();
    }

    public void Dispose()
    {
        workService.OnStatusChanged -= StatusChanged;
    }

    async Task ShowContextMenu(MouseEventArgs args)
    {
        if( await userService.PolicyApplies(PolicyDefinitions.s_IsEditor) )
        {
            var menuList = new List<ContextMenuItem>();

            if( ProcessingStatus.Status == JobStatus.Paused )
            {
                menuList.Add(new ContextMenuItem { Text = "Resume Processing", Value = 1 });
            }
            else
            {
                menuList.Add(new ContextMenuItem { Text = "Pause Processing", Value = 0 });
            }

            contextMenuService.Open(args, menuList, async args =>
            {
                contextMenuService.Close();

                switch( args.Value )
                {
                    case 0:
                        await workService.Pause(true);
                        break;
                    case 1:
                        await workService.Pause(false);
                        break;
                }
            });
        }
    }

}